package GUI;


import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.ClientConsole;

import Library.History;

/**
 * @author mishel hen
 *
 */
/**
 * This class display Active/History loans 
 */
public class GUI_LoanStudent extends JPanel
{
	/**
	 * Combo Box
	 */
	private JComboBox loanBox = null;
	/**
	 * 0- Active/1-History  
	 */
	private String loanType = "0";
	/**
	 * client Connected
	 */
	private ClientConsole cc;
	/**
	 * panel for student loans
	 */
	private GUI_LoanStudent ls = this;
	/**
	 * @param clc 
	 * @param mms  
	 */
	public GUI_LoanStudent(ClientConsole clc, final GUI_MainMenuStudent mms)
	{
		super();	
		cc = clc;
		this.setSize(700, 600);
		
		JLabel lblTitle = new JLabel("Loans");
		lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 24));

		lblTitle.setBounds(60, 60, 200, 40);
		add(lblTitle);
		
		
		
		this.add(getloanBox(), null);
		
		JButton btnSearch = new JButton("display");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String s[]={"loans student",cc.userName,loanType};
				try {
					cc.client.sendToServer(s);
					while(cc.client.flag==0)
					    System.out.print("");
					    cc.client.flag=0;
					    History history =(History)cc.client.message;
					    if(history.historyList.isEmpty())
								JOptionPane.showMessageDialog(new JFrame(),"No Lending");
						    else
						    {
						    	GUI_LendingHistoryStudent lhs= new GUI_LendingHistoryStudent(cc,history.historyList,mms);
			     		    	cc.cgui.setContentPane(lhs);
						    }
					  
					    	
				} catch (IOException e) {
					e.printStackTrace();
				}	// send message to server

				
		}});
		
		JButton btnBack = new JButton("Back");
		
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cc.cgui.setContentPane(mms);
			}
		})
		;
		btnBack.setBounds(240, 320, 120, 30);
		add(btnBack);
	setLayout(null);
	btnSearch.setBounds(60, 320, 120, 30);
	this.add(btnSearch);
	this.setVisible(true);		


}
	

	private JComboBox getloanBox() {
		if (loanBox == null) {
			loanBox = new JComboBox();
			loanBox.setBounds(new Rectangle(60, 210, 120, 25));  
				
			loanBox.addItem("Active ");
			loanBox.addItem("History");
		

				
			loanBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					Integer temp = loanBox.getSelectedIndex();
					loanType = temp.toString();
				}
			});
		}
		return loanBox;
	}
}